package rmiserver;

import java.awt.BorderLayout;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import rmiserver.service.RMIServiceImpl;
import rmiserver.ui.controller.DefaultController;
import rmiserver.ui.model.ConnectionModel;
import rmiserver.ui.view.DisplayViewPanel;

public class RMIServer {
	private DefaultController controller;
	private DisplayViewPanel panel;
	public RMIServer() {
		controller = new DefaultController();
		panel = new DisplayViewPanel(controller);
		controller.addView(panel);
		controller.addModel(new ConnectionModel());
	}
	public static void main(String[] args) throws Exception {
		RMIServer server = new RMIServer();
		server.initServer();
		server.initUI();
	}

	private void initServer() throws RemoteException, MalformedURLException {
		RMIServiceImpl service = new RMIServiceImpl(controller);
		Registry registry = LocateRegistry.createRegistry(1234);
		String s = "//127.0.1.1/rmiExample";
		registry.rebind(s, service);
	}

	private void initUI() {
		JFrame displayFrame = new JFrame();
		displayFrame.getContentPane().add(panel, BorderLayout.CENTER);
		displayFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		displayFrame.pack();
		displayFrame.setVisible(true);
	}
}
